package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kotlin.jvm.internal.Lambda;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.testng.internal.IParameterInfo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    /**
     * 实现步骤：
     * 1.判断是否是修改
     * 2.调用saveOrUpdate方法
     * 3.如果是更新，删除中间表数据
     * 4.最终进行中间表的数据的批量保存
     * @param roomSubmitVo
     */
    @Override
    public void customSaveOrUpdate(RoomSubmitVo roomSubmitVo) {
        //1.判断是否是修改
        boolean isUpdate=roomSubmitVo.getId()!=null;
        //2.调用公寓的saveOrUpdate方法
        saveOrUpdate(roomSubmitVo);
        //3.如果是更新，删除中间表数据
        if (isUpdate){
            //图片
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            lambdaQueryWrapper.eq(GraphInfo::getItemId,roomSubmitVo.getId());
            graphInfoService.remove(lambdaQueryWrapper);
            //房间配置
            LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,roomSubmitVo.getId());
            roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
            //房间标签
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomSubmitVo.getId());
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //房间支付方式
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomSubmitVo.getId());
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //房间属性
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomSubmitVo.getId());
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //房间租期
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomSubmitVo.getId());
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        //4.最终进行中间表的数据的批量保存
        //1.图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)){
            List<GraphInfo> graphInfoList=new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo:graphVoList){
                GraphInfo graphInfo=new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomSubmitVo.getId());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }
        //2.房间配置
        List<Long> facilityInfoIds=roomSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)){
            List<RoomFacility> roomFacilityList=new ArrayList<>(facilityInfoIds.size());
            for (Long facilityInfoId:facilityInfoIds){
                RoomFacility roomFacility=RoomFacility.builder().facilityId(facilityInfoId).roomId(roomSubmitVo.getId()).build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //3.房间标签
        List<Long> labelIds=roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            List<RoomLabel> roomLabelList=new ArrayList<>(labelIds.size());
            for (Long labelId:labelIds){
                RoomLabel roomLabel=RoomLabel.builder().labelId(labelId).roomId(roomSubmitVo.getId()).build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //4.房间支付方式
        List<Long> paymentTypeIds=roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(paymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList=new ArrayList<>(paymentTypeIds.size());
            for (Long paymentTypeId:paymentTypeIds){
                RoomPaymentType roomPaymentType=RoomPaymentType.builder().paymentTypeId(paymentTypeId).roomId(roomSubmitVo.getId()).build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //5.房间属性
        List<Long> attrValueIds=roomSubmitVo.getAttrValueIds();
        if(!CollectionUtils.isEmpty(attrValueIds)){
            List<RoomAttrValue> roomAttrValueList=new ArrayList<>(attrValueIds.size());
            for (Long attrValueId:attrValueIds){
                RoomAttrValue roomAttrValue=RoomAttrValue.builder().attrValueId(attrValueId).roomId(roomSubmitVo.getId()).build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //6.房间租期
        List<Long> leaseTermIds=roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(leaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTermList=new ArrayList<>(leaseTermIds.size());
            for (Long leaseTermId:leaseTermIds){
                RoomLeaseTerm roomLeaseTerm=RoomLeaseTerm.builder().leaseTermId(leaseTermId).roomId(roomSubmitVo.getId()).build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
    }


    @Override
    public void customPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.customSelectPage(page,queryVo);
    }
    /**
     1. 先根据房间id查询房间详情对象
     *        2. 根据房间id查询图片的vo对象 (定义mapper方法和mapper方法调用)
     *        3. 根据房间id查询配套的对象  (定义mapper方法和mapper方法调用)
     *        4. 根据房间id查询标签的对象  (定义mapper方法和mapper方法调用)
     *        5. 根据房间id查询房间支付方式的Vo对象 (定义mapper方法和mapper方法调用)
     *        6.根据房间的id查询房间属性的 Vo对象 (定义mapper方法和mapper方法调用)
     *        7.根据房间的id查询房间租期的 Vo对象 (定义mapper方法和mapper方法调用)
     * @param
     * @param
     */
    @Override
    public RoomDetailVo customDetail(Long id) {
        //1. 先根据房间id查询公寓详情对象
        RoomInfo roomInfo=getById(id);
        //查询所属公寓信息
        ApartmentInfo apartmentInfo=apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //2. 根据房间id查询图片的vo对象 (定义mapper方法和mapper方法调用)
        List<GraphVo> graphVoList=graphInfoMapper.selectGraphVoList(id,ItemType.ROOM);
        //3.根据房间的id查询配套的 Vo对象 (定义mapper方法和mapper方法调用)
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.roomSelectList(id);
        //4. 根据房间id查询标签的对象  (定义mapper方法和mapper方法调用)
        List<LabelInfo> labelInfoList=labelInfoMapper.roomSelectList(id);
        //5. 根据房间id查询房间支付方式的Vo对象 (定义mapper方法和mapper方法调用)
        List<PaymentType> paymentTypeList=paymentTypeMapper.roomSelectList(id);
        //6. 根据房间的id查询房间属性的 Vo对象 (定义mapper方法和mapper方法调用)
        List<AttrValueVo> attrValueVoList=roomAttrValueMapper.roomSelectList(id);
        //7.根据房间的id查询房间租期的 Vo对象 (定义mapper方法和mapper方法调用)
        List<LeaseTerm> leaseTermList=roomLeaseTermMapper.roomSelectList(id);
        //8.将数据合并到结果Vo中返回即可
        RoomDetailVo roomDetailVo=new RoomDetailVo();
        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        return roomDetailVo;
    }
    /**
     *  1. 根据id删除房间
     *  2. 删除中间表的数据 (图片 item_type item_id | apartment_id )
     * @param id
     */
    @Override
    public void customRemoveById(Long id) {
        //2.根据id删除房间
        removeById(id);
        //避免孤儿数据
        //3.删除中间表的数据（图片 item_type item_id | apartment_id ）
        //图片
        LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
        graphInfoService.remove(lambdaQueryWrapper);
        //配置
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //标签
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //支付方式
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //房间属性
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //租期
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper=new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }
}




